package exercise;

public class exerciseSort {
    //插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    //希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            //进行排序
            gap /= 2;
            shell(array, gap);

        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] >= tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    //选择排序
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, minIndex, i);
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void selectSortPlus(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int j = left + 1; j <= right; j++) {
                if (array[j] > array[maxIndex]) {
                    maxIndex = j;
                }
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, maxIndex, right);
            //如果结尾就是最小的数字，则minIndex一直指向right位置
            //但是maxIndex和right已经交换，所以此时maxIndex就是
            //最小值的位置
            if (minIndex == right) {
                minIndex = maxIndex;
            }
            swap(array, minIndex, left);
            left++;
            right--;
        }
    }

    public static void heapSort(int[] array) {
        //首先建立大根堆
        createBigHeap(array);
        //交换堆顶和堆尾,之后向下调整
        int end = array.length - 1;
        while (end >= 0) {
            swap(array, 0, end);
            siftDown(array, 0, end - 1);
            end--;
        }

    }

    private static void createBigHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            siftDown(array, parent, array.length - 1);
        }
    }

    private static void siftDown(int[] array, int parent, int end) {
        int child = (parent * 2) + 1;
        while (child <= end) {
            if (child < end && array[child] < array[child + 1]) {
                child++;
            }
            //此时child指向最大的孩子
            if (array[parent] < array[child]) {
                swap(array, child, parent);
                parent = child;
                child = (parent * 2) + 1;
            } else {
                break;
            }
        }
    }

    //冒泡排序
    public static void bubbleSort(int[] array) {
        boolean flg = true;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flg = false;
                }
            }
            if (flg) {
                break;
            }
        }
    }

    //快速排序
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        if(end - start < 10){
            insertQuick(array,start,end);
            return;
        }
        int mid = midTreeNum(array, start, end);
        swap(array, start, mid);
        int par = partition(array, start, end);
        quick(array, start, par - 1);
        quick(array, par + 1, end);

    }

    private static void insertQuick(int[] array, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    private static int midTreeNum(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if (array[left] > array[right]) {
            if (array[mid] > array[left]) {
                return left;
            } else if (array[mid] < array[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (array[mid] < array[left]) {
                return left;
            } else if (array[mid] > array[right]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    //归并排序
    public static void mergeSort(int[] array){
        mergeSortFun(array,0,array.length-1);
    }
    private static void mergeSortFun(int[] array,int left,int right){
        if(left >= right){
            return;
        }
        //先进行拆分
        int mid = (left + right) / 2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid+1,right);
        //再进行合并
        merge(array,left,mid,right);
    }
    private static void merge(int[] array,int left,int mid,int right){
        int ls = left;
        int le = mid;
        int rs = mid+1;
        int re = right;
        int k = 0;
        int[] arrK = new int[right - left +1];
        while(ls <= le && rs <= re){
            if(array[ls] <= array[rs]){
                arrK[k++] = array[ls++];
            }else{
                arrK[k++] = array[rs++];
            }
        }
        while(ls <= le){
            arrK[k++] = array[ls++];
        }
        while(rs <= re){
            arrK[k++] = array[rs++];
        }
        for(int i = 0;i<arrK.length;i++){
            array[left+i] = arrK[i];
        }
    }

}
